package com.lanou3g.baidumusic.main.mainactivity;

import android.app.Service;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.io.IOException;

/**
 * Created by dllo on 17/3/27.
 */

public class MusicService extends Service {

    private MusicBinder mMusicBinder = new MusicBinder();
    private CountDownTimer mTimer;
    private MediaPlayer mPlayer = new MediaPlayer();
    private boolean isTimerCount;
    private boolean isFirst = true ;

    @Override
    public void onCreate() {
        super.onCreate();

        mTimer = new CountDownTimer(Long.MAX_VALUE, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // 定时发送广播
                Intent intent = new Intent("musicBroad");
                int position = mPlayer.getCurrentPosition();
                int secTotal = position / 1000;
                int min = secTotal / 60;
                int sec = secTotal % 60;
                intent.putExtra("progress", "" + min + ":" + sec);
                sendBroadcast(intent);
            }

            @Override
            public void onFinish() {
                isTimerCount = false;
            }
        };
    }

    public void startTimer() {
        if (false == isTimerCount) {
            mTimer.start();
            isTimerCount = true;
        }
    }

    public void stopTimer() {
        if (true == isTimerCount) {
            mTimer.cancel();
            isTimerCount = false;
        }
    }



    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mMusicBinder;
    }

    public class MusicBinder extends Binder {
        public void play(String urlStr) {
            mPlayer.reset();
            try {
                // 播放本地歌曲(课替换为网路歌曲)
                AssetFileDescriptor fileDescriptor = getAssets().openFd("whzg.mp3");
                mPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
                fileDescriptor.close();
                mPlayer.prepare();
                mPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
            startTimer();
        }


        // 这是播放网络歌曲的方法
        public void playFromNet(String url) {
            mPlayer.reset();
            try {
                mPlayer.setDataSource(url);
                mPlayer.prepareAsync();
                mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mPlayer.start();
                    }
                });

                isFirst = false;
            } catch (IOException e) {
                e.printStackTrace();
            }
            // completion();
            startTimer();
        }


        //暂停的方法
        public void pause(){
            mPlayer.pause();
        }

        //判断现在是否播放
        public boolean isPlaying(){
            return mPlayer.isPlaying();
        }

        //判断是否是第一次播放
        public boolean isFirst(){
            return isFirst ;
        }

        //继续播放
        public void continuePlay(){
            mPlayer.start();
        }


        //给进度条用的
        public void setSongPosition(int position){
            mPlayer.seekTo(position);
        }












    }
}
